﻿using BUGCOME.Model.Common;
using BUGCOME.Model.Enum.File;
using BUGCOME.Model.File;
using BUGCOME.Model.File.Dto;
using BUGCOME.Model.File.Outher;
using Microsoft.AspNetCore.Http;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;

namespace BUGCOME.Service.File.IServices
{
    /// <summary>
    /// 文件管理服务接口
    /// </summary>
    public interface IFileBaseService : IBaseService<FileBase>
    {
        #region 基础CRUD
        /// <summary>
        /// 分页查询文件列表
        /// </summary>
        Task<PagedInfo<FileVo>> SelectFileListAsync(FileQueryDto dto);

        /// <summary>
        /// 根据ID查询文件详情
        /// </summary>
        Task<FileVo> SelectFileByIdAsync(long fileId);

        /// <summary>
        /// 新增文件元数据
        /// </summary>
        Task<long> InsertFileAsync(FileDto dto, long currentUserId, StoreType storeType = StoreType.LOCAL);

        /// <summary>
        /// 修改文件信息
        /// </summary>
        Task<int> UpdateFileAsync(FileDto dto, long currentUserId,string currentUserName);

        /// <summary>
        /// 批量删除文件
        /// </summary>
        Task<int> DeleteFileByIdsAsync(long[] fileIds);
        #endregion

        #region 上传下载
        /// <summary>
        /// 校验文件哈希是否已存在
        /// </summary>
        Task<bool> CheckFileHashExistsAsync(string fileHash);

        /// <summary>
        /// 将文件保存到本地存储的异步方法
        /// </summary>
        /// <param name="savePath">文件保存的目标路径（目录）</param>
        /// <param name="uploadDto">包含上传相关信息的数据传输对象</param>
        /// <param name="finalFileName">文件最终保存的名称（包含扩展名）</param>
        /// <param name="currentUserId">当前操作用户的ID</param>
        /// <param name="currentUserName">当前操作用户的名称</param>
        /// <returns>保存成功后返回文件信息对象，失败则返回null</returns>
        /// <exception cref="DirectoryNotFoundException">当指定的保存目录不存在时抛出</exception>
        /// <exception cref="IOException">文件写入过程中发生I/O错误时抛出</exception>
        Task<FileBase?> SaveFileToLocalAsync(string savePath, UploadDto uploadDto, string finalFileName, long currentUserId, string currentUserName);

        /// <summary>
        ///  将文件保存到阿里云存储的异步方法
        /// </summary>
        /// <param name="sysFile"></param>
        /// <param name="uploadDto"></param>
        /// <param name="file"></param>
        /// <returns></returns>
        Task<FileBase> SaveFileToAliyunAsync(FileBase sysFile, UploadDto uploadDto, IFormFile file);


        /// <summary>
        /// 文件下载（含个人/非个人权限判断）
        /// </summary>
        Task<FileDownloadResult> DownloadFileAsync(long fileId, long userId);

        /// <summary>
        /// 断点续传下载
        /// </summary>
        Task<FileRangeDownloadResult> RangeDownloadFileAsync(long fileId, long userId, string rangeHeader);
        #endregion

        #region 分片上传
        /// <summary>
        /// 分片上传初始化
        /// </summary>
        Task<MultipartUploadInitResult> InitMultipartUploadAsync(MultipartUploadInitDto dto, long userId);

        /// <summary>
        /// 上传分片
        /// </summary>
        Task UploadPartAsync(MultipartUploadPartDto dto);

        /// <summary>
        /// 合并分片
        /// </summary>
        Task<FileUploadResult> CompleteMultipartUploadAsync(MultipartUploadCompleteDto dto, long userId);
        #endregion

        #region 统计与权限
        /// <summary>
        /// 获取文件下载统计
        /// </summary>
        Task<FileDownloadStatsVo> GetFileDownloadStatsAsync(FileStatsQueryDto dto);

        /// <summary>
        /// 校验文件访问权限（区分个人/非个人）
        /// </summary>
        Task<FileAccessResult> CheckFileAccessAsync(long fileId, long userId);
        
        #endregion
    }
}